package com.railway.switchs.utils;

import org.apache.commons.codec.digest.DigestUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName Md5Util
 * @Author 魏梓晟
 * @Version 1.0
 **/
public class Md5Util {

    public final static  int SALTLEN = 6;

    /**
     * 加验加密 返回盐值和加密后的值
     * @param str
     * @return
     */
    public static Map<String,String> encryption(String str){
        Map<String,String> md5Map = new HashMap<>();
        String salt = StringUtil.getRandomStr(SALTLEN);
        md5Map.put("salt",salt);
        md5Map.put("md5",md5(str,salt));
        return md5Map;
    }

    /**
     * 加密验证
     * @param str
     * @param salt
     * @param md5Str
     * @return
     */
    public static boolean verify(String str,String salt,String md5Str){
        String md5Check = md5(str, salt);
        if(md5Check.equalsIgnoreCase(md5Str))
        {
            return true;
        }
        return false;
    }

    /**
     * 加密
     * @param str
     * @param salt
     * @return
     */
    public static String md5(String str, String salt){
        return DigestUtils.md5Hex(str + salt);
    }

    public static void main(String[] args) {
        Map<String,String> map = encryption("123456");
        System.out.println(map.get("md5"));
        System.out.println(map.get("salt"));
//        System.out.println(verify("5k109m9z3H","2y9qc5","e13c472719b6b5696f8488d87716f747"));
    }

}
